<template>
  <el-dialog title="修改开标时间" :visible.sync="dialogFormVisible" @close="resetForm">
    <el-form ref="editForm" :model="editForm" :rules="editRules" label-width="120px">
      <el-form-item label="采购项目名称" prop="projectName">
        <div class="app-text-grey-color__text">{{ editForm.projectName }}</div>
      </el-form-item>
      <el-form-item label="批次编号" prop="batchCode">
        <div class="app-text-grey-color__text">{{ editForm.batchCode }}</div>
      </el-form-item>
      <el-form-item label="原开标时间" prop="bidTime">
        <div class="app-text-grey-color__text">{{ editForm.bidTime }}</div>
      </el-form-item>
      <el-form-item label="新开标时间" prop="newBidTime">
        <el-date-picker v-model="editForm.newBidTime" type="date" format="yyyy/M/d" placeholder="请选择新的开标时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button type="primary" @click="confirmEdit" :loading="editLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import api from '../api/index.js';
export default {
  props: {
  },
  computed: {
  },
  data() {
    return {
      dialogFormVisible: false,
      editForm: {
        id: '',
        projectName: '',
        batchCode: '',
        bidTime: '',
        newBidTime: '',
      },
      editLoading: false,
      editRules: {
        newBidTime: [
          { required: true, message: '请选择新的开标时间', trigger: 'blur' },
        ],
      },
    }
  },
  watch: {},
  created() {
  },
  mounted() { },
  methods: {
    open(formData = {}) {
      this.editForm = {
        ...formData,
        newBidTime: '',
      };
      this.dialogFormVisible = true;
    },
    confirmEdit() {
      this.$refs['editForm'].validate((result) => {
        if (result) {
          const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
          const weekDay = weekDays[new Date(this.editForm.newBidTime).getDay()];
          const newBidTime = this.$utils.dateFormat(this.editForm.newBidTime, 'YYYY/m/d') + weekDay;
          if (this.editForm.bidTime === newBidTime) {
            this.$message.warning("新开标时间不可以与原开标时间一致");
            return;
          }
          this.editLoading = true;
          api.updateSecondPurchaseBatch({
            id: this.editForm.id,
            bidTime: newBidTime,
          })
            .then((res) => {
              this.editLoading = false;
              if (res.success) {
                this.dialogFormVisible = false;
                this.$refs['editForm'].resetFields();
                this.$message.success("开标时间修改成功");
                this.$emit('success');
              } else {
                this.$message.error('开标时间修改失败：' + (res.message || ''));
              }
            })
            .catch((e) => {
              this.editLoading = false;
              this.$log.error('开标时间修改失败，错误信息为：', e);
            });
        }
      });
    },
    resetForm() {
      this.$refs['editForm'].resetFields();
      this.dialogFormVisible = false;
    },
  }
}
</script>
<style lang="scss" scoped></style>